package it.poste.il.compensatore.msgsannullamento;

import it.poste.il.compensatore.utilities.SendSOAPMessage;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;

public class AnnulloRimborsoPerEstinzioneInput {

	SendSOAPMessage sendSOAPMessage;
	
	public SendSOAPMessage getSendSOAPMessage() {
		return sendSOAPMessage;
	}

	public void setSendSOAPMessage(SendSOAPMessage sendSOAPMessage) {
		this.sendSOAPMessage = sendSOAPMessage;
	}

	public String sendSoap(String soapAction, int timeout, String url, String source, String requestID, 
			String routingRole, String creationTime, String frazionario, String dataContabile, 
			String numeroSportello, String numeroOperazione) {
		
		String soapResponse = null;
		MessageFactory mf;
		try {
			mf = MessageFactory.newInstance();
			SOAPMessage sm = mf.createMessage();
			
			SOAPHeader sh = sm.getSOAPHeader();
			QName headerName = new QName("http://www.posteitaliane.it/ESBNG/Common/SOEenvelope", "ENVELOPE", "m");
			SOAPHeaderElement headerElement = sh.addHeaderElement(headerName);
			QName qnh1 = new QName("http://www.posteitaliane.it/ESBNG/Common/SOEenvelope", "source", "m");
			QName qnh2 = new QName("http://www.posteitaliane.it/ESBNG/Common/SOEenvelope", "requestID", "m");
			QName qnh3 = new QName("http://www.posteitaliane.it/ESBNG/Common/SOEenvelope", "routingRole", "m");
			QName qnh4 = new QName("http://www.posteitaliane.it/ESBNG/Common/SOEenvelope", "creationTime", "m");
			SOAPElement elementh1 = headerElement.addChildElement(qnh1);
			SOAPElement elementh2 = headerElement.addChildElement(qnh2);
			SOAPElement elementh3 = headerElement.addChildElement(qnh3);
		 	SOAPElement elementh4 = headerElement.addChildElement(qnh4);
		 	elementh1.addTextNode(source);
		  	elementh2.addTextNode(requestID);
		  	elementh3.addTextNode(routingRole);
		  	elementh4.addTextNode(creationTime);
		  	
			SOAPBody sb = sm.getSOAPBody();
			QName bodyName = new QName("http://www.posteitaliane.it/esb/schema/cmf/RimborsoPerEstinzione", "annulloRimborsoPerEstinzioneInput", "m");
			SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
			QName qnb = new QName("http://www.posteitaliane.it/esb/schema/cmf/RimborsoPerEstinzione", "DatiUfficioPostale", "m");
			SOAPElement datiUfficio = bodyElement.addChildElement(qnb);
			QName qnb1 = new QName("http://www.posteitaliane.it/esb/schema/cmf/RimborsoPerEstinzione", "frazionario", "m");
			QName qnb2 = new QName("http://www.posteitaliane.it/esb/schema/cmf/RimborsoPerEstinzione", "dataContabile", "m");
			QName qnb3 = new QName("http://www.posteitaliane.it/esb/schema/cmf/RimborsoPerEstinzione", "numeroSportello", "m");
			QName qnb4 = new QName("http://www.posteitaliane.it/esb/schema/cmf/RimborsoPerEstinzione", "numeroOperazione", "m");
			SOAPElement elementb1 = datiUfficio.addChildElement(qnb1);
			SOAPElement elementb2 = datiUfficio.addChildElement(qnb2);
			SOAPElement elementb3 = datiUfficio.addChildElement(qnb3);
		 	SOAPElement elementb4 = datiUfficio.addChildElement(qnb4);
		 	elementb1.addTextNode(frazionario);
		  	elementb2.addTextNode(dataContabile);
		  	elementb3.addTextNode(numeroSportello);
		  	elementb4.addTextNode(numeroOperazione);

		  	soapResponse = sendSOAPMessage.sendSOAPMessage(sm, url, soapAction, timeout);

		} catch (SOAPException e) {
			System.out.println("Errore SOAPException nel send della richiesta soap: " + e.getMessage());
			System.out.println("Lo stack trace e': ");
			e.printStackTrace();
		}
		
		return soapResponse;
	}
}
